package com.abe.libquick.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.abe.libcore.base.screen.j.AbeJActivity;
import com.abe.libquick.R;
import com.abe.libquick.utils.ConstantMBLib;
import com.abe.libquick.utils.NormalUtils;
import com.abe.libquick.utils.SpQuickHelper;
import com.abe.libquick.utils.quick.StatusBarUtil;
import com.abe.libquick.widget.dialog.DateTimePickerDialog2;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Locale;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AbeJActivity {

    public String getEmpty(String value) {
        return TextUtils.isEmpty(value) ? "" : value;
    }

    public String getText(EditText editText) {
        if (editText == null) return "";
        return editText.getText().toString().trim();
    }

    public String getText(TextView textView) {
        if (textView == null) return "";
        return textView.getText().toString().trim();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        ButterKnife.bind(this);
    }

    @Override
    public void onConfigurationChanged(@NotNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration newConfig = res.getConfiguration();
        newConfig.fontScale = SpQuickHelper.fontScale();//设置标准
        res.updateConfiguration(newConfig, res.getDisplayMetrics());
        return res;
    }

    public void setStatusBarColor() {
        StatusBarUtil.setStatusBar(this, getResources().getColor(R.color.colorPrimary));
    }

    @Override
    public Boolean isAutoZoom() {
        return false;
    }

    public Bundle bundle() {
        return getIntent().getExtras();
    }

    public void localEmptyMsg(int what) {
        getHandler().sendEmptyMessage(what);
    }

    public Date stringToDate(String dateStr, String rule) {
        return NormalUtils.stringToDate(rule, dateStr);
    }

    public String dateToString(Date date, String rule) {
        return NormalUtils.dateToString(rule, date);
    }

    public String dateToString(String rule, Date date) {
        return NormalUtils.dateToString(rule, date);
    }

    public void toast(int strRes) {
        toast(getResources().getString(strRes));
    }

    public void toast(String rule, Object... objects) {
        toast(String.format(Locale.CHINA, rule, objects));
    }

    /**
     * 日期相关操作
     */
    protected void bindDateDialog(final TextView view) {
        view.setOnClickListener(v -> showDateAndTimeDialog(view, DateTimePickerDialog2.TYPE_DATE));
    }

    //时间相关操作
    protected void bindTimeDialog(final TextView view) {
        view.setOnClickListener(v -> showDateAndTimeDialog(view, DateTimePickerDialog2.TYPE_TIME));
    }

    //日期与时间
    protected void bindDateAndTimeDialog(final TextView view) {
        view.setOnClickListener(v -> showDateAndTimeDialog(view, DateTimePickerDialog2.TYPE_DATE_TIME));
    }

    /**
     * 选择日期
     *
     * @param view 绑定标签
     * @param type 类型
     *             type:[DateTimePickerDialog2.TYPE_DATE_TIME,DateTimePickerDialog2
     *             .TYPE_TIME,DateTimePickerDialog2.TYPE_DATE]
     */
    protected void showDateAndTimeDialog(TextView view, int type) {
        DateTimePickerDialog2 dateDialog = new DateTimePickerDialog2(this, type, view);
        dateDialog.show();
    }

    //屏幕截图
    protected void capture() {
        toast("正在截取屏幕图片...");
        getWindow().getDecorView().setDrawingCacheEnabled(true);
        Bitmap bmp = getWindow().getDecorView().getDrawingCache();
        try {
            File file = new File(ConstantMBLib.MAP_SCREENSHOT_PATH + new Date().getTime() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            toast("屏幕截图成功，图片存在: %s", file.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}